package com.hue.concurrent.day05;

/**
 * 使用wait、notify控制两个线程的执行顺序
 */
public class Demo03 {
    public static Object lock = new Object();
    public static boolean flag = false;

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                while (!flag) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("t1执行");
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("t2执行");
                flag = true;
                lock.notify();
            }
        });

        t1.start();
        t2.start();
    }
}
